package org.web.ssm.controller.wechatController;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.DocumentException;
import org.web.ssm.model.wechatModel.CheckModel;
import org.web.ssm.model.wechatModel.TulingRequest;
import org.web.ssm.service.IOrderService;
import org.web.ssm.service.wechatService.ICheckoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.web.ssm.service.wechatService.ITulingService;
import org.web.ssm.utils.MessageUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by LUOLIANG on 2016/6/1.
 */
@RestController
@RequestMapping("/WechatDocking")
public class WeDockingController {
    private Logger logger = LogManager.getLogger(WeDockingController.class);
    @Autowired
    private ICheckoutService checkoutService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ITulingService tulingService;

    @RequestMapping(value = "/check", method = {RequestMethod.GET, RequestMethod.POST})
    public String wechat(CheckModel checkModel, HttpServletRequest request, HttpServletResponse response) {
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        if (isGet) {
            String echostr = checkout(checkModel);

            return echostr;
        } else {
            // 进入POST接收消息处理
            System.out.println("enter post");
            // 接收消息并返回消息
            String message = acceptMessage(request, response);

            return message;
        }
    }

    public String checkout(CheckModel checkModel) {

        System.out.println(checkModel.toString());

        return checkoutService.checkSignature(checkModel);
    }

    /**
     * 接收用户消息接口
     *
     * @param request
     * @param response
     * @return
     */
    public String acceptMessage(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> map = null;
        try {
            map = MessageUtil.xmlToMap(request);
            String FromUserName = map.get("FromUserName");
            String ToUserName = map.get("ToUserName");
            String MsgType = map.get("MsgType");
            String Content = map.get("Content");
            String message = null;

            if (MessageUtil.MESSAGE_TEXT.equals(MsgType)) {
                if (Content.contains("赚到") || Content.contains("收入")) {
                    message = MessageUtil.initText(ToUserName, FromUserName, orderService.messageOrder(Content, "收入", FromUserName));
                } else if ("菜单".equals(Content.trim()) || "menu".equals(Content.trim())) {
                    message = MessageUtil.initText(ToUserName, FromUserName,
                            MessageUtil.menuText());
                } else if (Content.contains("吃饭") || Content.contains("买") || Content.contains("用") || Content.contains("零食") || Content.contains("支出")) {
                    message = MessageUtil.initText(ToUserName, FromUserName, orderService.messageOrder(Content, "支出", FromUserName));
                } else {
                    message = MessageUtil.initText(ToUserName, FromUserName, tulingService.getTextMessage(new TulingRequest(Content, FromUserName)));
                }
            } else if (MessageUtil.MESSAGE_EVENT.equals(MsgType)) {
                String eventType = map.get("Event");
                if (MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)) {
                    message = MessageUtil.initText(ToUserName, FromUserName,
                            MessageUtil.menuText());
                }
            }
            System.out.println(message);
            return message;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return "SUCCESS";
    }
}
